package mesweb.mesweb.controller.device;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.farvision.common.api.CommonResult;
import com.farvision.common.exception.ApiException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import mesweb.mesweb.entity.device.MesDeviceParameter;
import mesweb.mesweb.service.impl.device.MesDeviceParameterServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author niufw
 * @since 2021-05-19
 */
@RestController
@RequestMapping("/mes/mes-device-parameter")
@Api(tags = "MesDeviceParameterController", description = "设备参数管理")

public class MesDeviceParameterController {

    @Autowired
    private MesDeviceParameterServiceImpl MesDeviceParameterService;

    @GetMapping("/GetDeviceParameters")
    @ApiOperation("获取设备列表")
    public CommonResult<IPage<MesDeviceParameter>> GetDevices(@ApiParam("设备id") @RequestParam( value = "deviceId",required = false) Integer deviceId,
                                                       @ApiParam("当前页") @RequestParam( value = "page",required = false) Integer page,
                                                       @ApiParam("每页行数") @RequestParam( value = "size",required = false) Integer size)
    {
//        QueryWrapper<MesDeviceParameter> queryWrapper = new QueryWrapper<>();
//        if(deviceId != 0) {
//            queryWrapper.eq("DeviceId", deviceId);
//        }
        if(null == deviceId)
        {
            deviceId = 0;
        }
        page = page == null || page < 1 ? 1 : page;
        size = size == null || size < 0 ? 2 : size;
        Page<MesDeviceParameter> MesDeviceParameterPage = new Page<>(page,size);
        IPage<MesDeviceParameter> data =  MesDeviceParameterService.getDeviceParameters(MesDeviceParameterPage,deviceId);
        return  CommonResult.success(data);
    }

    @GetMapping("/GetDeviceParameter")
    @ApiOperation("根据编号获取设备参数")
    public CommonResult<MesDeviceParameter> GetDeviceByNo(@ApiParam("设备参数编号") @RequestParam("no") String no)
    {
        if(no.isEmpty())
            return null;
        QueryWrapper<MesDeviceParameter> queryWrapper = new QueryWrapper<MesDeviceParameter>();
        queryWrapper.eq("DeviceParaNo",no);
        return CommonResult.success(MesDeviceParameterService.getOne(queryWrapper));
    }
    @PostMapping("/SaveDeviceParameter")
    @ApiOperation("保存设备参数")
    public CommonResult<Boolean> SaveDevice(@ApiParam("设备参数") @RequestBody MesDeviceParameter mesDeviceParameter) {
        if (mesDeviceParameter == null) {
            return null;
        }

        try {
            boolean result = false;
            if (mesDeviceParameter.id <= 0l) {
                QueryWrapper<MesDeviceParameter> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("device_PARA_No", mesDeviceParameter.getDeviceParaNo());
                queryWrapper.or().eq("device_PARA_NAME", mesDeviceParameter.getDeviceParaName());
                MesDeviceParameter mesTechnologyFlow1 = MesDeviceParameterService.getOne(queryWrapper);
                if (mesTechnologyFlow1 != null) {
                    String msg = StrUtil.format("编号{}或名称{}已经存在！", mesTechnologyFlow1.getDeviceParaNo(), mesTechnologyFlow1.getDeviceParaName());
                    throw new ApiException(msg);
                }
                result = MesDeviceParameterService.save(mesDeviceParameter);
            }else{
               result = MesDeviceParameterService.update(mesDeviceParameter,null);
            }
            return CommonResult.success(result);
        } catch (Exception ex) {
            return CommonResult.failed(ex.getMessage());
        }
    }
    @DeleteMapping("/DeleteDeviceParameter/{Id}")
    @ApiOperation("删除设备参数")
    public CommonResult<Boolean> DeleteDevice(@ApiParam("设备参数ID") @PathVariable("Id") int Id) {
        try {
            boolean result = MesDeviceParameterService.removeById(Id);
            return CommonResult.success(result);
        } catch (Exception ex) {
            return CommonResult.failed(ex.getMessage());
        }
    }
}
